const httpclient = require('../../utils/httpclient.js')
//获取应用实例
var app = getApp()
Page({
  data: {
    addressData:{
      gender:'1'
    }
  },
  bindCancel:function () {
    wx.navigateBack({})
  },
  bindData:function(e){
    var value = e.detail.value;
    var name = e.currentTarget.dataset.bind;
    var names = name.split('.');
    var _data = this.data;
    var currentObject = _data;
    for(var i=0;i<names.length;i++){
      if(i != names.length-1){
        currentObject = currentObject[names[i]];
      }
    }
    currentObject[names[names.length - 1]] = value;
  },
  radioChange: function (e) {
    this.data.addressData.gender = e.detail.value;
  },
  bindSave: function(e) {
    var that = this;
    var name = that.data.addressData.name;
    var tel = that.data.addressData.tel;
    var gender = that.data.addressData.gender;
    var address = that.data.addressData.address; 
    var no = that.data.addressData.no;
    var longitude = that.data.addressData.longitude;
    var latitude = that.data.addressData.latitude;

    if (typeof (name)=='undefined' || name == ""){
      wx.showModal({
        title: '提示',
        content: '请填写联系人姓名',
        showCancel:false
      })
      return
    }
    if (typeof (tel) == 'undefined' || tel == ""){
      wx.showModal({
        title: '提示',
        content: '请填写手机号码',
        showCancel:false
      })
      return
    }
    if (typeof (gender) == 'undefined' || gender == "") {
      wx.showModal({
        title: '提示',
        content: '请选择一个性别',
        showCancel: false
      })
      return
    }
    if (typeof (address) == 'undefined' || address == ""){
      wx.showModal({
        title: '提示',
        content: '请填写详细地址',
        showCancel:false
      })
      return
    }
    if (typeof (no) == 'undefined' || no == "") {
      wx.showModal({
        title: '提示',
        content: '请填写楼号信息',
        showCancel: false
      })
      return
    }

    // 验证全部完成后开始创建或修改
    // id存在就修改
    // id不存在就创建
    httpclient.req(
      "/mall/add_user_consignee_info",
      {
        user_id: app.globalData.userInfo.id,
        name: name,
        address: address,
        no: no,
        tel: tel,
        gender: gender,
        id: that.data.addressData.id,
        longitude: longitude,
        latitude: latitude
      },
      'post',
      function (res) {
        if (res.data.result) {
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2];  //上一个页面
          var curAddressData = prevPage.data.curAddressData;
          curAddressData = {
            name: name,
            address: address,
            no: no,
            tel: tel,
            gender: gender
          };

          //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
          prevPage.setData({
            curAddressData: curAddressData
          })
          wx.navigateBack({});
        }
      },
      function (result) {
        console.log(result)
      }
    );
  },
  onLoad: function (e) {
    var that = this;
    var id = e.id;
    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({
      success: function (res) {
        var n = 750 / res.windowWidth;
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });

    if (id) {
      // 初始化原数据
      wx.showLoading();
      httpclient.req(
        "/mall/query_one_consignee_info",
        {
          id: id
        },
        'post',
        function (res) {
          if (res.data) {
            that.setData({
              addressData: res.data
            });
            wx.hideLoading();
          }
        },
        function (result) {
          wx.hideLoading();
        }
      );
    }
  },
  selectAddress: function(e){
    wx.navigateTo({
      url: '/pages/find-address/index'
    })
  }
})
